package gui;

import graphNew.DinicBasedAlgo;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import main.ProgramOptions;
import main.ProgramOptions.UnSplitable;

import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JRadioButtonMenuItem;
/**
 * 
 * @author dannysi
 *The MinCut options Gui
 */
public class DinicGui extends JFrame {

	private JPanel contentPane;
	private JTextField untilText;
	private boolean raiseEdges=false;
	public static DinicGui frame; 
	private JTextField rankLimitText;
	private boolean largerCut=true;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new DinicGui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DinicGui() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 591, 287);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		untilText = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.EAST, untilText, -316, SpringLayout.EAST, contentPane);
		contentPane.add(untilText);
		untilText.setColumns(10);
		untilText.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar()<'0' || e.getKeyChar()>'9')
					e.consume();
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				
			}
		});
		
		JButton btnGo = new JButton("Go");
		sl_contentPane.putConstraint(SpringLayout.SOUTH, btnGo, -10, SpringLayout.SOUTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, btnGo, -186, SpringLayout.EAST, contentPane);
		btnGo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (raiseEdges){
					ProgramOptions.optionUnsplitable=UnSplitable.TRY_AGAIN;
					try{
					ProgramOptions.keepTrying=Integer.parseInt(untilText.getText());
					}catch (NumberFormatException ex){
						ProgramOptions.keepTrying=100;
					}
				}else
					ProgramOptions.optionUnsplitable=UnSplitable.LEAVE;
				if (largerCut)
					ProgramOptions.cutChoise=ProgramOptions.CutChoise.LARGE;
				else
					ProgramOptions.cutChoise=ProgramOptions.CutChoise.SMALL;
				try{
					ProgramOptions.rankLimit=Integer.parseInt(rankLimitText.getText());
				}catch (NumberFormatException ex){
					ProgramOptions.rankLimit=-1;
				}
				DinicGui.frame.setVisible(false);
				try {
					DinicBasedAlgo.mainAlgo();
				} catch (NumberFormatException e1) {
					System.out.println("ERROR");
				} catch (Exception e1) {
					System.out.println("ERROR");
				}
			}
		});
		contentPane.add(btnGo);
		
		final JLabel untilLbl = new JLabel("until");
		sl_contentPane.putConstraint(SpringLayout.NORTH, untilText, -1, SpringLayout.NORTH, untilLbl);
		sl_contentPane.putConstraint(SpringLayout.WEST, untilText, 18, SpringLayout.EAST, untilLbl);
		untilLbl.setEnabled(false);
		untilLbl.setFont(new Font("Tahoma", Font.BOLD, 13));
		contentPane.add(untilLbl);
		
		JLabel lblWhenSplitDoesnt = new JLabel("When Split Doesnt Succeed");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblWhenSplitDoesnt, 10, SpringLayout.NORTH, contentPane);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblWhenSplitDoesnt, 27, SpringLayout.WEST, contentPane);
		lblWhenSplitDoesnt.setFont(new Font("Tahoma", Font.BOLD, 15));
		contentPane.add(lblWhenSplitDoesnt);
		
		final JRadioButton stopSplittingBtn = new JRadioButton("Stop Splitting");
		stopSplittingBtn.setFont(new Font("Tahoma", Font.BOLD, 13));
		sl_contentPane.putConstraint(SpringLayout.NORTH, stopSplittingBtn, 25, SpringLayout.SOUTH, lblWhenSplitDoesnt);
		sl_contentPane.putConstraint(SpringLayout.WEST, stopSplittingBtn, 10, SpringLayout.WEST, contentPane);
		contentPane.add(stopSplittingBtn);
		
		final JRadioButton raiseEdgesBtn = new JRadioButton("Raise Edge Edges");
		sl_contentPane.putConstraint(SpringLayout.NORTH, untilLbl, 4, SpringLayout.NORTH, raiseEdgesBtn);
		sl_contentPane.putConstraint(SpringLayout.WEST, untilLbl, 6, SpringLayout.EAST, raiseEdgesBtn);
		sl_contentPane.putConstraint(SpringLayout.NORTH, raiseEdgesBtn, 18, SpringLayout.SOUTH, stopSplittingBtn);
		sl_contentPane.putConstraint(SpringLayout.WEST, raiseEdgesBtn, 0, SpringLayout.WEST, stopSplittingBtn);
		raiseEdgesBtn.setFont(new Font("Tahoma", Font.BOLD, 13));
		contentPane.add(raiseEdgesBtn);
		
		JButton button = new JButton("<--Beck");
		sl_contentPane.putConstraint(SpringLayout.NORTH, button, 0, SpringLayout.NORTH, btnGo);
		sl_contentPane.putConstraint(SpringLayout.EAST, button, -34, SpringLayout.WEST, btnGo);
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Gui2.window.frame.setVisible(true);
				DinicGui.frame.setVisible(false);
			}
		});
		contentPane.add(button);
		
		rankLimitText = new JTextField();
		sl_contentPane.putConstraint(SpringLayout.EAST, rankLimitText, -358, SpringLayout.EAST, contentPane);
		contentPane.add(rankLimitText);
		rankLimitText.setColumns(10);
		
		rankLimitText.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				if (e.getKeyChar()<'0' || e.getKeyChar()>'9')
					e.consume();
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				
			}
		});
		
		
		JLabel lblRankLimit = new JLabel("Rank Limit");
		sl_contentPane.putConstraint(SpringLayout.WEST, rankLimitText, 6, SpringLayout.EAST, lblRankLimit);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, rankLimitText, 0, SpringLayout.SOUTH, lblRankLimit);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblRankLimit, 35, SpringLayout.SOUTH, raiseEdgesBtn);
		lblRankLimit.setFont(new Font("Tahoma", Font.BOLD, 13));
		sl_contentPane.putConstraint(SpringLayout.EAST, lblRankLimit, 0, SpringLayout.EAST, stopSplittingBtn);
		contentPane.add(lblRankLimit);
		
		JLabel lblCandidateToSplit = new JLabel("Candidate To Split");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblCandidateToSplit, 0, SpringLayout.NORTH, lblWhenSplitDoesnt);
		sl_contentPane.putConstraint(SpringLayout.EAST, lblCandidateToSplit, -69, SpringLayout.EAST, contentPane);
		lblCandidateToSplit.setFont(new Font("Tahoma", Font.BOLD, 15));
		contentPane.add(lblCandidateToSplit);
		
		final JRadioButton largerCutBtn = new JRadioButton("Larger Cut");
		
		largerCutBtn.setFont(new Font("Tahoma", Font.BOLD, 13));
		sl_contentPane.putConstraint(SpringLayout.NORTH, largerCutBtn, 0, SpringLayout.NORTH, stopSplittingBtn);
		sl_contentPane.putConstraint(SpringLayout.WEST, largerCutBtn, 0, SpringLayout.WEST, lblCandidateToSplit);
		contentPane.add(largerCutBtn);
		largerCutBtn.doClick();
		
		final JRadioButton smallerCutBtn = new JRadioButton("Smaller Cut");
		smallerCutBtn.setFont(new Font("Tahoma", Font.BOLD, 13));
		sl_contentPane.putConstraint(SpringLayout.NORTH, smallerCutBtn, 0, SpringLayout.NORTH, raiseEdgesBtn);
		sl_contentPane.putConstraint(SpringLayout.WEST, smallerCutBtn, 0, SpringLayout.WEST, lblCandidateToSplit);
		contentPane.add(smallerCutBtn);
		
		stopSplittingBtn.doClick();
		
		smallerCutBtn.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (!largerCut)
					smallerCutBtn.doClick(); 
				if (largerCut){
					largerCutBtn.doClick();
					largerCut=false;
					
				}
			}
		});
		
		stopSplittingBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (!raiseEdges)
					stopSplittingBtn.doClick(); 
				if (raiseEdges){
					raiseEdgesBtn.doClick();
					raiseEdges=false;
					untilLbl.setEnabled(false);
					untilText.setEnabled(false);
				}
				
				
			}
		});
		
		largerCutBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (largerCut)
					largerCutBtn.doClick(); 
				if (!largerCut){
					smallerCutBtn.doClick();
					largerCut=true;
					
				}
				
			}
		});
		
		raiseEdgesBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (raiseEdges)
					raiseEdgesBtn.doClick(); 
				if (!raiseEdges){
					stopSplittingBtn.doClick();
					raiseEdges=true;
					untilLbl.setEnabled(true);
					untilText.setEnabled(true);
				}
			}
		});
		
		
				
				
		
	}
}
